package com.cea.papyrus.core.actions.imports;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.gef.commands.Command;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.common.edit.command.ChangeCommand;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.VisibilityKind;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.commands.EMFtoGEFWrapCommand;
import com.cea.papyrus.core.ui.dialogs.PackageImportTreeSelectionDialog;
import com.cea.papyrus.core.utils.LookForElement;

/**
 * Action that imports package from workspace models
 */
public class ImportPackageFromCurrentModelAction extends ImportPackageAction {

	/** request key for the request associated to this action  */
	public final static String IMPORT_PACKAGE_FROM_CURRENT_MODEL_ACTION_REQUEST = ImportPackageFromCurrentModelAction.class.getName()+ "Request";

	/** ID of  this action */
	public final static String IMPORT_PACKAGE_FROM_CURRENT_MODEL_ACTION_ID = ImportPackageFromCurrentModelAction.class.getName()+ "ID";
	
	/**
	 * Creates a new ImportLibraryFromRepositoryAction
	 * @param part that part which contains the selection
	 */
	public ImportPackageFromCurrentModelAction(IWorkbenchPart part) {
		super(part);
		setText("Import Package from current model");
		setId(IMPORT_PACKAGE_FROM_CURRENT_MODEL_ACTION_ID);
		setToolTipText("Import Package from current model");

		// icon for action
		ImageDescriptor descriptor = PapyrusPlugin.getImageDescriptor(PapyrusPlugin.PAPYRUS_ICONS_16x16+"ImportPackageCurrentModel.gif");
		setImageDescriptor(descriptor);
		setHoverImageDescriptor(descriptor);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected boolean calculateEnabled() {
		// look the selection.
		// if one of the element is a package and is not an imported package, it is enabled
		return(getSelectedPackage()!=null);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public Command createCommand(EditingDomain editingDomain) {
		return new ImportPackageCommand(editingDomain);
	}

	
	/**
	 * EMF change Command wrapped in a GEF command to import packages from Workspace.
	 */
	public class ImportPackageCommand extends EMFtoGEFWrapCommand {
		
		/**
		 * Creates a new ImportPackageCommand.
		 * @param editingDomain editing doamin used by the EMF command
		 */
		public ImportPackageCommand(EditingDomain editingDomain) {
			super(editingDomain);
		}
		
		/**
		 * {@inheritDoc}
		 */
		public org.eclipse.emf.common.command.Command createEMFCommand() {
			Runnable runnable = new Runnable() {
				public void run() {
					final Package selectedPackage = getSelectedPackage();
					if(selectedPackage!=null) {
						PackageImportTreeSelectionDialog eisd =
							new PackageImportTreeSelectionDialog(PlatformUI.getWorkbench()
									.getActiveWorkbenchWindow().getShell(), LookForElement
									.topPackage(selectedPackage));
						int ret = eisd.open();

						if (Window.OK == ret) {
							ArrayList result = eisd.getResult();
							Iterator resultIter = result.iterator();
							while (resultIter.hasNext()) {
								Element element = (Element) resultIter.next();
								selectedPackage.createPackageImport((Package)element, VisibilityKind.PUBLIC_LITERAL);
							}
						}
					}
				}
			};
			return new ChangeCommand(editingDomain, runnable, "Import Packages");
		}
	}
	
}